package com.cat.dataStructure02;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

class NumberContainers {
    Map<Integer, Integer> m1 = new HashMap<>();
    Map<Integer, PriorityQueue<Integer>> m2 = new HashMap<>();

    public NumberContainers() {
    }
    
    public void change(int index, int number) {
        // index : number
        m1.put(index, number);
        // 给number 加入一个 index
        m2.computeIfAbsent(number, x -> new PriorityQueue<>()).add(index);
    }
    
    public int find(int number) {
        PriorityQueue<Integer> queue = m2.get(number);
        if (queue == null) {
            return -1;
        }
        while (!queue.isEmpty() && m1.get(queue.peek()) != number) {    // 位置对不上
            queue.poll();
        }
        return queue.isEmpty() ? -1 : queue.peek();
    }
}

/**
 * Your NumberContainers object will be instantiated and called as such:
 * NumberContainers obj = new NumberContainers();
 * obj.change(index,number);
 * int param_2 = obj.find(number);
 */